﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public  class JSDWFApplyDAL
    {
        /// <summary>
        /// 添加图纸申请
        /// </summary>
        /// <param name="ApplyUserName"></param>
        /// <param name="Remark"></param>
        /// <param name="DWFIDS"></param>
        /// <returns></returns>
        public static int Add(string ApplyUserName,int AdtlvlID, string Remark, string DWFIDS)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_Add", ApplyUserName,AdtlvlID, Remark, DWFIDS);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 审批完成,通过
        /// </summary>
        /// <param name="ApplyGUID"></param>
        public static void CompleteFlowPass(Guid ApplyGUID)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_CompleteFlowPass",ApplyGUID);
            DB.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 审批完成,拒绝
        /// </summary>
        /// <param name="ApplyGUID"></param>
        /// <param name="Remark"></param>
        public static void CompleteFlowReject(Guid ApplyGUID, string Remark)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_CompleteFlowReject", ApplyGUID, Remark);
            DB.ExecuteNonQuery(dbCommand);
        }

        public static IDataReader GetByApplyGUID(Guid ApplyGUID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_GetByApplyGUID", ApplyGUID);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 获取相应审批明细
        /// </summary>
        /// <param name="ApplyGUID"></param>
        /// <returns></returns>
        public static DataTable GetDetailByApplyGUID(Guid ApplyGUID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_GetDetailByApplyGUID", ApplyGUID);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        /// <summary>
        /// 根据用户名查看此用户目前正在审请的图纸
        /// </summary>
        /// <param name="ApplyGUID"></param>
        /// <returns></returns>
        public static IDataReader GetDWFApplyByUserName(string UserName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_GetDWFApplyByUserName", UserName);
            return DB.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// 获取需发送的邮件 的申批 JSDWFApply
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetNeedSendEmail()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_GetNeedSendEmail");
            return DB.ExecuteReader(dbCommand);
	    }
        /// <summary>
        /// 进入下一个审批流程
        /// </summary>
        /// <param name="ApplyGUID"></param>
        /// <param name="NextFlowID"></param>
        /// <returns></returns>
        public static bool UpdateNextFlow(Guid ApplyGUID, int NextFlowID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_UpdateNextFlow", ApplyGUID, NextFlowID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 更新邮件状态为已发送
        /// </summary>
        /// <param name="ApplyGUID"></param>
        /// <returns></returns>
        public static bool UpdateSendEmailState(Guid ApplyGUID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_UpdateSendEmailState", ApplyGUID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        /// <summary>
        /// 获取申请历史列表
        /// </summary>
        /// <param name="WhereSql"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("JSDWFApply_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
    }
}
